package com.wtwd.device.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.wtwd.device.constant.Constant;
import com.wtwd.device.constant.UserSourceEnum;
import com.wtwd.device.dao.ThirdPartyUserMapper;
import com.wtwd.device.model.entity.AppUserInfo;
import com.wtwd.device.model.entity.GoogleUser;
import com.wtwd.device.model.entity.ThirdPartyUser;
import com.wtwd.device.model.vo.LoginVO;
import com.wtwd.device.service.AppUserService;
import com.wtwd.device.service.RedisService;
import com.wtwd.device.service.ThirdPartyUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zdl
 * @date 2021/11/15 10:08
 */
@Service
public class ThirdPartyUserServiceImpl implements ThirdPartyUserService {


    @Resource
    private ThirdPartyUserMapper thirdPartyUserMapper;

    @Resource
    private AppUserInfoServiceImpl appUserInfoService;

    @Resource
    private RedisService redisService;

    @Resource
    private AppUserService appUserService;

    @Override
    public LoginVO loginForIdTokenTemplate(String idToken) {
        GetTokenResponseBase tokenResponseBase = checkIdToken(idToken);
        if (tokenResponseBase ==null) {
            return null;
        }
        String userKey = getUserKey(tokenResponseBase);
        ThirdPartyUser user = getUserByUserKey(userKey);
        boolean isFirst = user == null;
        Long userId;
        if (isFirst) {
            ThirdPartyUserInfo userInfo = getUserInfo(tokenResponseBase);
            userId = saveUser(userInfo);
        } else {
            userId = user.getUserId();
        }
        LoginVO loginVO = null;
        if (userId != null) {
            loginVO = doLogin(userId);
            loginVO.setIsFirst(isFirst);
        }
        return loginVO;
    }

    @Override
    public GetTokenResponseBase checkIdToken(String idToken) {
        return  null;
    }

    @Override
    public ThirdPartyUser getUserByUserKey(String userKey) {
        ThirdPartyUser user = new LambdaQueryChainWrapper<>(thirdPartyUserMapper)
                .eq(ThirdPartyUser::getUserKey, userKey)
                .one();
        return user;
    }

    @Override
    public ThirdPartyUserInfo getUserInfo(GetTokenResponseBase getTokenResponseBase) {
        return null;
    }

    @Override
    public Long saveUser(ThirdPartyUserInfo userInfo) {
        ThirdPartyUser thirdPartyUser = new GoogleUser();
        thirdPartyUser.setUserKey(userInfo.getUserKey());
        thirdPartyUserMapper.insert(thirdPartyUser);
        Constant.THREAD_LOCAL_TABLE.remove();
        Long userId = thirdPartyUser.getUserId();
        AppUserInfo iuserInfo = appUserService.getDefaultObj(userId);
        String name = userInfo.getName();
        if (StringUtils.isNotBlank(name)) iuserInfo.setName(name);
        iuserInfo.setUserType(UserSourceEnum.GOOGLE.getCode());
        return appUserInfoService.save(iuserInfo) ? userId : null;
    }

    @Override
    public String getUserKey(GetTokenResponseBase getTokenResponseBase) {
        return null;
    }

    @Override
    public GetTokenResponseBase getAccessToken(String code) {
        return null;
    }

    @Override
    public LoginVO doLogin(Long userId) {
        return redisService.createLoginToken(userId);
    }
}
